//自定义图标
var pIcon = new BMap.Icon("/sharepark/mobile/images/p.png", new BMap.Size(40, 40));
var mIcon = new BMap.Icon("/sharepark/mobile/images/mark.png", new BMap.Size(40, 40));

// 百度地图API功能
var map = new BMap.Map("allmap");

//开启鼠标滚轮缩放
map.enableScrollWheelZoom(true);

//搜索结果展示
var options = {
    onSearchComplete: function(results) {
        var s = "";
        if(results.getCurrentNumPois() > 0){
            // 判断状态是否正确
            if (local.getStatus() == BMAP_STATUS_SUCCESS) {
                $(".yj-footer").hide();
                for (var i = 0; i < results.getCurrentNumPois(); i++) {
                    s += '<div class="card" onclick="mark(\'' + results.getPoi(i).point.lng + '\',\'' + results.getPoi(i).point.lat + '\')" >' +
                        '<div class="card-right"><img src="mobile/images/dingwei.png" width="30px"></div>'+
                        '<p class="">' + results.getPoi(i).title + '</p>' +
                        '地址: ' + results.getPoi(i).address + '</div>';
                }
                $("#result").html(s);

            }
        }else {
            s = '<div class="card" style="font-size: 16px"><i class="layui-icon layui-icon-face-cry" style="font-size: 18px; color: #FF5722;"></i> 没有搜索到相关地址</div>';
            $("#result").html(s);
        }

        $("#content").show();
    }
};

//定位设置中心地区
var local;
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
        map.centerAndZoom(r.point, 15);
        map.setMapStyleV2({
            styleId: '1c0ef29f0200462080c02daa151811dd'
        });
        var mk = new BMap.Marker(r.point);
        map.addOverlay(mk);
        map.panTo(r.point);
        local = new BMap.LocalSearch(map,options);
    } else {
        alert('定位失败：错误码: ' + this.getStatus());
    }
}, {enableHighAccuracy: true});

//自己定位的方法
function LocalMe() {
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mk = new BMap.Marker(r.point);
            map.addOverlay(mk);
        } else {
            alert('定位失败：错误码: ' + this.getStatus());
        }
    }, {enableHighAccuracy: true});
}


// 编写自定义函数,创建标注
function addMarker(point,c) {
    var marker = new BMap.Marker(point, {icon: pIcon});
    //给每个图标添加点击事件
    marker.addEventListener("click",showPark);
    //点击地图图标展示车位列表
    function showPark() {
        $(".yj-footer").hide();
        //通过小区查找车位
        var sp = "<div class='card'>小区名：" + c.comName + "<br>小区地址：" + c.comAddress + "</div>";
        $.get("/sharepark/lessor/findParkByComId",{comId:c.comId},function(data){
            //将查询出来的车位渲染到页面
            for (var i = 0;i < data.length;i++){
                var park = data[i];
                sp += "<div class='card'><a href='goods_detail.html?id=" + park.id + "'>" +
                    '<div class="card-right"><img src="mobile/images/qiang.png" width="30px"></div>'+
                    "<p>车位信息：" + park.cAddress + "</p>" +
                    "价格：" + park.cPrice + "/小时</a></div>";
            }
            $("#result").html(sp);
            $("#content").show();

        })
    }
    //绘制点
    map.addOverlay(marker);
}

//获取数据库所有坐标
function allParrk(){
    $.get("/sharepark/community/mapList", function (data) {
        for (var i = 0; i < data.length; i++) {
            var c = data[i];
            var sw = c.comLongitude;
            var ne = c.comLatitude;
            var nowPoint = new BMap.Point(sw, ne);
            addMarker(nowPoint,c);
        }
    });
}
allParrk();

//点击搜索结果后的操作
function mark(lng,lat) {
    map.clearOverlays();
    $("#content").hide();
    $(".yj-footer").show();
    var point = new BMap.Point(lng,lat);    //获取第一个智能搜索的结果
    var mk = new BMap.Marker(point,{icon: mIcon});
    map.centerAndZoom(point, 15);
    map.addOverlay(mk);
    map.panTo(point);
    LocalMe();
    allParrk();
}

//搜索方法
function s() {
    blurInput();
    local.search($("#suggestId").val());
    $("#suggestId").val("");
}

//关闭搜索结果面板
function c() {
    $("#content").hide();
    $(".yj-footer").show();
}

//取消输入框焦点的方法
function blurInput(){
    $("#suggestId").blur();
}

//点击地图取消输入框焦点
map.addEventListener("click", blurInput);
